package servlets;

import java.io.IOException;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import structures.AD;
import managers.DatabaseManager;
import managers.JSonManager;

public class AddFeedbackServlet  extends HttpServlet {

	private static final long serialVersionUID = 7836831441991411804L;

	protected void doPut(HttpServletRequest request, HttpServletResponse response) {
		String idAdvertisement, feedbackText, feedbackValue, jsonResult;
		AD advertisement;
		DatabaseManager databaseManager;
		JSonManager jSonManager;
		jSonManager = new JSonManager();
		databaseManager = new DatabaseManager();
		idAdvertisement = request.getParameter("idAdvertisement");
		feedbackText = request.getParameter("feedbackText");
		feedbackValue = request.getParameter("feedbackValue");
		
         try {
        	databaseManager.connect();
        	advertisement = databaseManager.SetFeedback(idAdvertisement, feedbackText, feedbackValue);
        	jsonResult = jSonManager.generateJSON(advertisement);
        	response.setContentType("application/json");
    		response.setCharacterEncoding("UTF-8");
			response.getWriter().write(jsonResult);
			databaseManager.closeConnection();
	        
        } catch (Exception e) {
			e.printStackTrace();
			try {
				response.getWriter().write("{\n\t\"errors\":\"yes\"\n}");
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		}
    }
}
